package com.xn.app.muying.util;

import android.content.Context;

import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xn.app.muying.bean.WxPayVo;

/**
 * 微信支付封装
 */
public class WeChatPayUtils {
    private Context mContext;

    //微信分配的公众账号ID
    public static final String APP_ID = "wx055fabfaac2ae00d";
    //微信支付分配的商户号
    private static final String PARTNER_ID = "1418243402";

    public Boolean isSupported = false;

    private IWXAPI msgApi;

    public WeChatPayUtils(Context context) {
        mContext = context;
        msgApi = WXAPIFactory.createWXAPI(context, APP_ID);
        isSupported = msgApi.isWXAppInstalled() && msgApi.isWXAppSupportAPI();
    }

    /**
     * 发起支付
     *
     * @param wxPayVo 支付实体
     */
    public void pay(WxPayVo wxPayVo) {
        PayReq request = new PayReq();

        request.appId = APP_ID;

        request.partnerId = PARTNER_ID;
        request.packageValue = "Sign=WXPay";
        if (wxPayVo != null) {
            request.prepayId = wxPayVo.prepayid;
            //随机字符串 后台返回
            request.nonceStr = wxPayVo.noncestr;
            //时间戳 后台返回
            request.timeStamp = wxPayVo.timeStamp;
            //签名 后台返回
            request.sign = wxPayVo.sign;
        }
        msgApi.registerApp(APP_ID);
        msgApi.sendReq(request);
    }

}
